function loadfile(filename,filetype,isAsync){

    if(filetype == "js"){
        var fileref = document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src",filename);
        if(isAsync){
            fileref.setAttribute("async",'true');
            fileref.setAttribute("crossorigin",'anonymous');
        }
    }else if(filetype == "css"){
        var fileref = document.createElement('link');
        fileref.setAttribute("rel","stylesheet");
        fileref.setAttribute("type","text/css");
        fileref.setAttribute("href",filename);
    }
   if(typeof fileref != "undefined"){
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
    
}
function wfIsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
        "SymbianOS", "Windows Phone",
        "iPad", "iPod"
    ];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}
function crossDomainAjax_post (url,successCallback,data) {

    url = encodeURI(url);
    // IE8 & 9 only Cross domain JSON GET request
    if ('XDomainRequest' in window && window.XDomainRequest !== null) {
        var xdr = new XDomainRequest(); // Use Microsoft XDR
        xdr.open('get', url);
        xdr.onload = function () {
            var dom  = new ActiveXObject('Microsoft.XMLDOM'),
                JSON = $.parseJSON(xdr.responseText);

            dom.async = false;

            if (JSON == null || typeof (JSON) == 'undefined') {
                JSON = $.parseJSON(data.firstChild.textContent);
            }

            successCallback(JSON); // internal function
        };

        xdr.onerror = function() {
            _result = false;
        };

        xdr.send();
    }

    // IE7 and lower can't do cross domain
    else if (navigator.userAgent.indexOf('MSIE') != -1 &&
        parseInt(navigator.userAgent.match(/MSIE ([\d.]+)/)[1], 10) < 8) {
        return false;
    }

    // Do normal jQuery AJAX for everything else
    else {
        $.ajax({
            url: url,
            cache: false,
            dataType: 'json',
            type: 'POST',
            data:data,
            async: false, // must be set to false
            success: function (data, success) {
                successCallback(data);
            }
        });
    }
}


function crossDomainAjax (url,successCallback) {

    url = encodeURI(url);
    // IE8 & 9 only Cross domain JSON GET request
    if ('XDomainRequest' in window && window.XDomainRequest !== null) {
        var xdr = new XDomainRequest(); // Use Microsoft XDR
        xdr.open('get', url);
        xdr.onload = function () {
            var dom  = new ActiveXObject('Microsoft.XMLDOM'),
                JSON = $.parseJSON(xdr.responseText);

            dom.async = false;

            if (JSON == null || typeof (JSON) == 'undefined') {
                JSON = $.parseJSON(data.firstChild.textContent);
            }

            successCallback(JSON); // internal function
        };

        xdr.onerror = function() {
            _result = false;
        };

        xdr.send();
    }

    // IE7 and lower can't do cross domain
    else if (navigator.userAgent.indexOf('MSIE') != -1 &&
        parseInt(navigator.userAgent.match(/MSIE ([\d.]+)/)[1], 10) < 8) {
        return false;
    }

    // Do normal jQuery AJAX for everything else
    else {
        $.ajax({
            url: url,
            cache: false,
            dataType: 'json',
            type: 'GET',
            async: false, // must be set to false
            success: function (data, success) {
                successCallback(data);
            }
        });
    }
}

(function(){
var host=document.domain; 
var hostArr = host.split(".");
var domain = "//img.mbalib.com";

if(hostArr[1] == 'dev')
{
  domain = "//img.dev.mbalib.com";
}
else if(hostArr[1] == 'test')
{
  domain = "//static.test.mbalib.com";
}else if(hostArr[1] == 'test2')
{
	domain = '//static.test2.mbalib.com';
}
loadfile(domain+"/common/servicesPicture/spread.css","css");
//loadfile(domain+"/common/a_mbalib/ad_v0927.js?v=1124","js");
if(typeof VipPay !== 'undefined' && (host == 'wiki.mbalib.com' || host == 'wiki.dev.mbalib.com' || host == 'wiki.test.mbalib.com')){
    var vipPayInstance = VipPay.getInstance();
    vipPayInstance.getUserInstance.getUser(function (result) {
        if(!result.is_vip){
            loadfile(domain+"/common/servicesPicture/prom.js?v=1124","js");
            if(wfIsPC()){
                loadfile("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8234842646771010","js",true);
            }
        }
        loadfile(domain+"/common/mbalib_statistics_v2.js?v=5235","js");
        loadfile(domain+"/common/reward.css","css");
        loadfile(domain+"/common/reward.js","js");
    })
} else {
    loadfile(domain+"/common/servicesPicture/prom.js?v=1124","js");
    loadfile(domain+"/common/mbalib_statistics_v2.js?v=5235","js");
    if(host == 'wiki.mbalib.com' || host == 'wiki.dev.mbalib.com')
    {
        if(wfIsPC()){
            loadfile("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8234842646771010","js",true);
        }
        loadfile(domain+"/common/reward.css","css");
        loadfile(domain+"/common/reward.js","js");
    }
}
})();
